package cc.lj.internal.mobilepay.application.service;

import cc.lj.internal.mobilepay.domain.model.vo.CiticLogDO;
import cc.lj.internal.mobilepay.domain.service.ICiticLogDomainService;
import cc.lj.internal.mobilepay.interfaces.dto.command.CiticRequestCommand;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 生成中信响应日志处理类
 * @author liangxifeng
 * @date 2022/11/25 15:44
 */
@Component
public class CreateCiticLog {
    //中信请求响应日志领域层
    @Resource(name = "citicLogDomainServiceImpl")
    private ICiticLogDomainService domainService;

    /**
     * 新增中信日志（中信请求）
     * @param reqCommand 外部请求入参
     * @param reqMap 请求中信map
     * @param logDO 日志实体DO
     * @return 新增后的主键
     */
    public Integer addLog(CiticRequestCommand reqCommand,Map<String, String> reqMap, CiticLogDO logDO) {
        logDO.setOutTradeNo(reqCommand.getOutTradeNo()); //商户号
        logDO.setPactId(reqCommand.getPactId()); //合同号或卡号
        logDO.setPerformType(reqCommand.getPerformType()); //平台类型
        logDO.setAddTime(LocalDateTime.now());
        logDO.setRequestBody(reqMap.toString());
        Integer lastId = domainService.save(logDO);
        return lastId;
    }
    /**
     * 修改中信日志(中信响应)
     * @param id 主键
     * @param resMap 中信响应map
     * @param updateLogDo 修改日志实体DO
     * @return 修改后的影响行数
     */
    public Integer updateLog(Integer id,Map<String,String> resMap,CiticLogDO updateLogDo) {
        updateLogDo.setId(id); //通过主键修改
        updateLogDo.setResponseBody(resMap.toString()); //响应信息
        updateLogDo.setResponseTime(LocalDateTime.now()); //响应时间
        updateLogDo.setReturnCode(resMap.get("return_code")); //返回码
        if(resMap.get("return_msg") != null) { //返回信息
            updateLogDo.setReturnMsg(resMap.get("return_msg"));
        }
        if(resMap.get("result_code") != null) {  //业务结果
            updateLogDo.setResultCode(resMap.get("result_code"));
        }
        if(resMap.get("err_code") != null) { //错误码
            updateLogDo.setErrCode(resMap.get("err_code"));
        }
        if(resMap.get("err_msg") != null) { //错误码描述
            updateLogDo.setErrMsg(resMap.get("err_msg"));
        }
        //修改日志
        return domainService.updateById(updateLogDo);
    }
}
